#include "server_library_object.h"

uint8 server_library_object_list_load_flag = 0;

// 图书馆物品信息存储链表
LibraryObjectList server_library_object_list = {
    .first_node = NULL,        // 初始化链表头指针为NULL
    .last_node = NULL,        // 初始化链表尾指针为NULL
    .total_objects = 0        // 初始化物品总数为0
};

/* 图书馆物品查找返回
    * 该函数用于根据物品ID查找图书馆中的物品
    * @param id 物品ID
    * @return 返回找到的物品指针，由于每个物品的ID唯一，所以找到即返回，如果未找到或出现异常则返回NULL
*/
LibraryObjectNode * server_find_object_by_id(uint32 id) {
    // 先检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return NULL; // 未加载物品列表
    }
    // 检查物品数量是否为0
    if (server_library_object_list.total_objects == 0) {
        server_log(LOG_SYSTEM, "No objects in the library. Object find function return NULL.");
        // 不再断言
        return NULL; // 返回空结果
    }
    // 遍历物品列表查找指定ID的物品
    LibraryObjectNode * current_node = server_library_object_list.first_node;
    while(current_node != NULL) {
        if (current_node->object.id == id) {
            // 找到物品
            return current_node;
        }
        current_node = current_node->next; // 移动到下一个节点
    }
    // 未找到物品
    server_log(LOG_INFO, "Object with ID %u not found in the library.", id);
    return NULL; // 返回空结果
}

/* 图书馆物品查找返回
    * 该函数用于根据物品名称/标题查找图书馆中的物品
    * @param name 物品名称
    * @param is_sensitive 是否大小写敏感
    * @return 返回找到的物品列表指针，如果未找到或出现异常则返回NULL
*/
LibraryObjectPtrList * server_find_object_by_incomplete_name(wchar_t * name, StringIsSensitive is_sensitive) {
    // 先检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return NULL; // 未加载物品列表
    }
    // 检查物品数量是否为0
    if (server_library_object_list.total_objects == 0) {
        server_log(LOG_INFO, "No objects in the library. Object find function return NULL.");
        return NULL; // 返回空结果
    }
    // 遍历物品列表查找指定名称的物品
    LibraryObjectNode       * current_node = server_library_object_list.first_node;
    LibraryObjectPtrList    * result_list = malloc(sizeof(LibraryObjectPtrList)); // 创建一个结果链表，存储物品指针
    if (result_list == NULL) {
        server_log(LOG_SYSTEM, "Memory allocation failed in server_find_object_by_incomplete_name.");
        server_assert(result_list == NULL);
        return NULL; // 内存分配失败
    }
    result_list->first_node = NULL; // 初始化结果链表指针
    result_list->last_node  = NULL;
    result_list->total_objects = 0; // 初始化结果链表物品总数
    while(current_node != NULL) {
        if (server_string_is_in(current_node->object.title, name, is_sensitive)) {
            LibraryObjectPtrNode * new_node = malloc(sizeof(LibraryObjectPtrNode)); // 找到物品，创建一个新节点
            if (new_node == NULL) {
                server_log(LOG_SYSTEM, "Memory allocation failed in server_find_object_by_incomplete_name.");
                free(result_list); // 释放已分配的结果链表内存
                server_assert(new_node == NULL);
                return NULL; // 内存分配失败
            }
            new_node->next      = NULL; // 初始化新节点的next指针
            new_node->prev      = result_list->last_node; // 设置前驱节点
            new_node->object    = &(current_node->object); // 设置物品指针
            if (result_list->last_node != NULL) {
                result_list->last_node->next = new_node; // 如果不是第一个节点，设置前驱节点的next指针
            } else {
                result_list->first_node = new_node; // 如果是第一个节点，设置头指针
            }
            result_list->last_node = new_node; // 更新尾指针
            result_list->total_objects++; // 增加物品总数
        }
        current_node = current_node->next; // 移动到下一个节点
    }
    if (result_list->total_objects == 0) {
        server_log(LOG_INFO, "No objects found with the name '%ls'.", name);
        free(result_list); // 释放结果链表内存
        return NULL; // 未找到物品，返回NULL
    }
    server_log(LOG_INFO, "Found %llu objects with the name '%ls'.", result_list->total_objects, name);
    return result_list; // 返回找到的物品列表
}

/* 图书馆物品增加
    * 该函数用于将物品添加到图书馆物品列表中
    * @param object 要添加的物品指针，不采用其中的ID，因为ID是唯一的，且由服务器自动分配
    * @return 如果添加成功返回0，失败或异常返回1
*/
uint8 server_add_object(LibraryObject * object) {
    // 检查物品是否为NULL
    if (object == NULL) {
        server_log(LOG_SYSTEM, "Cannot add object because the object pointer is NULL.");
        server_assert(object == NULL); // 断言物品为NULL
        return 1; // 如果物品为NULL，返回0
    }
    // 检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return 1; // 未加载物品列表，返回1
    }
    // 创建新的物品节点
    LibraryObjectNode * new_node = malloc(sizeof(LibraryObjectNode));
    if (new_node == NULL) {
        server_log(LOG_SYSTEM, "Memory allocation failed in server_add_object.");
        server_assert(new_node == NULL);
        return 1; // 内存分配失败，返回1
    }
    object->id = book_config.next_book_id++; // 分配新的物品ID

    new_node->object = *object; // 复制物品信息到新节点
    new_node->next = NULL; // 初始化新节点的next指针
    new_node->prev = server_library_object_list.last_node; // 设置前驱节点
 
    // 将新节点添加到链表中
    if (server_library_object_list.last_node != NULL) {
        server_library_object_list.last_node->next = new_node; // 如果不是第一个节点，设置前驱节点的next指针
    } else {
        server_library_object_list.first_node = new_node; // 如果是第一个节点，设置头指针
    }
    server_library_object_list.last_node = new_node; // 更新尾指针
    server_library_object_list.total_objects++; // 增加物品总数

    server_log(LOG_INFO, "Object with ID %u added to the library.", object->id);
    return 0; // 成功添加物品，返回0
}

/* 图书馆删除特定节点的物品
    * 该函数用于删除图书馆中的特定物品节点
    * @param delete_node 要删除的物品节点指针
    * @return 如果删除成功返回0，未找到物品或异常返回1
*/
uint8 server_delete_object_node(LibraryObjectNode * delete_object_node){
    if (delete_object_node == NULL) {
        server_log(LOG_SYSTEM, "Object cannot be deleted because the delete_node is NULL.");
        return 1; // 未找到物品，返回1
    }
    if (delete_object_node == server_library_object_list.first_node) {
        server_library_object_list.first_node = delete_object_node->next; // 更新头指针
        delete_object_node->next->prev = NULL; // 设置下一个节点的prev指针为NULL
    } else {
        delete_object_node->prev->next = delete_object_node->next; // 设置前驱节点的next指针
        if (delete_object_node->next != NULL) {
            delete_object_node->next->prev = delete_object_node->prev; // 设置下一个节点的prev指针
        } else {
            server_library_object_list.last_node = delete_object_node->prev; // 如果是最后一个节点，更新尾指针
        }
    }
    // 释放物品节点内存
    free(delete_object_node);
    server_library_object_list.total_objects--; // 减少物品总数
    
    return 0; // 成功删除物品，返回0
}

/* 图书馆删除特定ID的物品
    * 该函数用于根据物品ID删除图书馆中的物品
    * @param id 物品ID
    * @return 如果删除成功返回0，未找到物品或异常返回1
*/
uint8 server_delete_object_by_id(uint32 id) {
    // 先检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return 1; // 未加载物品列表
    }
    // 检查物品数量是否为0
    if (server_library_object_list.total_objects == 0) {
        server_log(LOG_INFO, "No objects in the library. Object delete function return 1.");
        return 1; // 返回1表示没有物品可删除
    }
    // 查找指定ID的物品
    LibraryObjectNode * delete_object_node = server_find_object_by_id(id);
    if (delete_object_node == NULL) {
        server_log(LOG_INFO, "Object with ID %u cannot be deleted because the find function return NULL.", id);
        return 1; // 未找到物品，返回1
    }
    // 删除物品节点
    server_delete_object_node(delete_object_node);
    server_log(LOG_INFO, "Object with ID %u deleted from the library.", id);
    return 0; // 成功删除物品，返回0
}

/* 图书馆物品修改
    * 该函数用于修改图书馆中的物品数据
    * @param object_node 要修改的物品节点指针
    * @param new_object_data 新的物品数据，不使用其中的ID，保持原来ID不变
    * @return 如果修改成功返回0，未找到物品或异常返回1
*/
uint8 server_modify_object(LibraryObjectNode * object_node, LibraryObject * new_object_data) {
    // 检查物品节点和新物品数据是否为NULL
    if (object_node == NULL || new_object_data == NULL) {
        server_log(LOG_SYSTEM, "Null object node or new object data passed to server_modify_object.");
        return 1; // 如果物品节点或新物品数据为NULL，返回1
    }
    // 更新物品信息
    // 保持ID不变
    uint32 old_id = object_node->object.id; // 保存旧ID
    object_node->object = *new_object_data; // 复制新物品数据到节点
    object_node->object.id = old_id; // 恢复旧ID
    server_log(LOG_INFO, "Object with ID %u modified in the library.", object_node->object.id);
    return 0; // 成功修改物品，返回0
}

/* 图书馆物品借还操作
    * 该函数用于借出图书馆中的物品
    * @param id 物品ID
    * @param count 借出数量
    * @param user 借出物品的用户信息
    * @return 根据情况返回对应枚举
*/
ObjectFunctionReturn    server_lend_object  (uint32 id, uint16 count, User * user_p){
    // 判空
    if (user_p == NULL) {
        server_log(LOG_SYSTEM, "User information is NULL in server_lend_object.");
        return BOOK_UNKNOWN_ERROR; // 用户信息为NULL，返回
    }
    // 先检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return BOOK_UNKNOWN_ERROR; // 未加载物品列表
    }
    // 检查借书权限
    if (user_p->permission.ObjectLendAuthority) {
        server_log(LOG_SYSTEM, "User %s does not have permission to lend objects.", user_p->name);
        return BOOK_LEND_PERMISSION_DENIED; // 用户没有借书权限，返回
    }
    // 检查用户借阅数量是否超限
    uint16 max_lent_count = 0;
    switch(user_p->type) {
        case USER_TYPE_STUDENT:
            max_lent_count = book_config.stu_max_borrow_books;
            break;
        case USER_TYPE_TEACHER:
            max_lent_count = book_config.teacher_max_borrow_books;
            break;
        case USER_TYPE_ADMINISTRATOR:
            max_lent_count = BOOK_MANAGE_CONFIG_MAX_BORROW_BOOKS_CONFIG; // 管理员可以借阅最大数量
            break;
        default:
            server_log(LOG_SYSTEM, "Unknown user type for user %s.", user_p->name);
            server_assert(!(user_p->type)); // 断言
            server_assert((user_p->type < USER_TYPE_STUDENT) || (user_p->type > USER_TYPE_ADMINISTRATOR)); // 断言
            return BOOK_UNKNOWN_ERROR; // 未知用户类型，返回
    }
    if (user_p->lent_object_count + count > max_lent_count) {
        server_log(LOG_SYSTEM, "The number of books that user %s can borrow is less than he want to borrow", user_p->name);
        return BOOK_LEND_NOT_ENOUGH_USER_COUNT; // 用户借阅数量超限，返回
    }
    // 查找指定ID的物品
    LibraryObjectNode * lend_object_node = server_find_object_by_id(id);
    if (lend_object_node == NULL) {
        server_log(LOG_SYSTEM, "Object with ID %u cannot be lent because the find function return NULL.", id);
        return BOOK_LEND_NOT_EXIST; // 未找到物品，返回
    }
    // 检查物品数量是否足够
    if (lend_object_node->object.remain_num < count) {
        server_log(LOG_SYSTEM, "Not enough objects available for lending. Requested: %u, Available: %u", count, lend_object_node->object.remain_num);
        return BOOK_LEND_NOT_ENOUGH_BOOK; // 物品数量不足，返回
    }
    // 能够借阅
    lend_object_node->object.remain_num -= count; // 减少物品剩余数量
    user_p->lent_object_count += count; // 增加用户借阅数量
    // 添加借阅记录
    for (uint16 i = 0; i < BOOK_MANAGE_CONFIG_MAX_BORROW_BOOKS_CONFIG; i++) {
        if (user_p->lent_objects[i].is_valid == 0) { // 找到一个空闲的借阅记录
            user_p->lent_objects[i].object_id = id; // 设置物品ID
            wcscpy(user_p->lent_objects[i].object_name, lend_object_node->object.title); // 设置物品名称
            user_p->lent_objects[i].count = count; // 设置借出数量
            server_get_current_date(&user_p->lent_objects[i].lent_date); // 设置借出日期
            user_p->lent_objects[i].is_valid = 1; // 标记为有效
            break; // 找到后退出循环
        }
    }
    server_log(LOG_INFO, "User %s lent %u of object ID %u successfully.", user_p->name, count, id);
    return BOOK_LEND_SUCCESS; // 成功借出物品，返回
}

/* 图书馆物品归还操作
    * 该函数用于归还图书馆中的物品
    * @param id 物品ID
    * @param count 归还数量
    * @return 根据情况返回对应枚举
*/
ObjectFunctionReturn  server_return_object(uint32 id, uint16 count, User * user_p){
    // 判空
    if (user_p == NULL) {
        server_log(LOG_SYSTEM, "User information is NULL in server_return_object.");
        server_assert(user_p == NULL); // 断言用户信息为NULL
        return BOOK_UNKNOWN_ERROR; // 用户信息为NULL，返回
    }
    // 先检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return BOOK_UNKNOWN_ERROR; // 未加载物品列表
    }
    // 查找指定ID的物品
    LibraryObjectNode * return_object_node = server_find_object_by_id(id);
    if (return_object_node == NULL) {
        server_log(LOG_SYSTEM, "Object with ID %u cannot be returned because the find function return NULL.", id);
        return BOOK_RETURN_NOT_EXIST; // 未找到物品，返回
    }
    // 检查归还数量是否合理
    if (count > return_object_node->object.total_num - return_object_node->object.remain_num) {
        server_log(LOG_SYSTEM, "Return count %u exceeds lent count for object ID %u.", count, id);
        return BOOK_RETURN_INVALID; // 归还数量超过借出数量，返回
    }
    uint8 record_deleted_flag = 0; // 借阅记录成功找到并删除
    
    // 用户还书可能是一块借，分批还，也可能是一块借，一块还，也可能是分批借，一块还，也可能是分批借，分批还
    for (uint16 i = 0; i < BOOK_MANAGE_CONFIG_MAX_BORROW_BOOKS_CONFIG; i++) {
        if (user_p->lent_objects[i].is_valid && user_p->lent_objects[i].object_id == id) { // 找到对应的借阅记录
            // 若归还数量等于记录中借书数量，删除借阅记录
            if (user_p->lent_objects[i].count == count) { // 归还数量等于借阅数量
                user_p->lent_objects[i].is_valid = 0; // 标记为无效
                user_p->lent_object_count -= count; // 减少用户借阅数量
                record_deleted_flag = 1; // 标记记录已删除
                server_log(LOG_INFO, "User %s returned %u of object ID %u successfully.", user_p->name, count, id);
                break; // 找到后退出循环
            } else if(user_p->lent_objects[i].count > count) { // 归还数量小于借阅数量
                user_p->lent_objects[i].count -= count; // 减少借阅数量
                user_p->lent_object_count -= count; // 减少用户借阅数量
                record_deleted_flag = 1; // 标记记录已删除
                server_log(LOG_INFO, "User %s partially returned %u of object ID %u successfully.", user_p->name, count, id);
                break; // 找到后退出循环
            } else {
                // 归还数量大于借阅数量，查找下一个记录
                count -= user_p->lent_objects[i].count;
                user_p->lent_objects[i].is_valid = 0; // 标记为无效
                user_p->lent_object_count -= user_p->lent_objects[i].count; // 减少用户借阅数量
                server_log(LOG_INFO, "User %s returned %u of object ID %u successfully, but still has %u left to return.", user_p->name, user_p->lent_objects[i].count, id, count);
                // 继续查找下一个借阅记录
                continue; // 继续查找下一个记录 这里显式使用continue
            }
        }
    }
    if (!record_deleted_flag) {
        server_log(LOG_SYSTEM, "No matching lent record found for user %s with object ID %u.", user_p->name, id);
        return BOOK_RETURN_INVALID; // 未找到匹配的借阅记录，返回
    }
    // 能够归还
    return_object_node->object.remain_num += count; // 增加物品剩余数量
    server_log(LOG_INFO, "Object with ID %u returned successfully. Count: %u", id, count);
    return BOOK_RETURN_SUCCESS; // 成功归还物品，返回
}

/* 图书馆物品续借操作
    * 该函数用于续借图书馆中的物品
    * @param record_list_number 借阅记录列表编号
    * @param user 借出物品的用户信息
    * @return 根据情况返回对应枚举
*/
ObjectFunctionReturn    server_renew_object(uint16 record_list_number, User * user_p){
    // 判空
    if (user_p == NULL) {
        server_log(LOG_SYSTEM, "User information is NULL in server_renew_object.");
        server_assert(user_p == NULL); // 断言用户信息为NULL
        return BOOK_UNKNOWN_ERROR; // 用户信息为NULL，返回
    }
    // 先检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return BOOK_UNKNOWN_ERROR; // 未加载物品列表
    }
    
    // 续借合法检查
    if (record_list_number >= BOOK_MANAGE_CONFIG_MAX_BORROW_BOOKS_CONFIG) {
        server_log(LOG_SYSTEM, "Invalid record list number: %u. Must be less than %d.", record_list_number, BOOK_MANAGE_CONFIG_MAX_BORROW_BOOKS_CONFIG);
        return BOOK_UNKNOWN_ERROR; // 记录列表编号无效，返回
    }
    if (!user_p->lent_objects[record_list_number].is_valid) {
        server_log(LOG_SYSTEM, "No valid lent record found for user %s at index %u.", user_p->name, record_list_number);
        return BOOK_UNKNOWN_ERROR; // 没有有效的借阅记录，返回
    }
    // 续借
    user_p->lent_objects[record_list_number].renew_count ++; // 增加续借次数
    // 分角色续借次数限制
    uint16 max_renew_count = 0;
    uint16 renew_days = 0;
    switch(user_p->type) {
        case USER_TYPE_STUDENT:
            max_renew_count = book_config.stu_renew_max_count;
            renew_days = book_config.stu_renew_days; // 学生续借天数
            break;
        case USER_TYPE_TEACHER:
            max_renew_count = book_config.teacher_renew_max_count;
            renew_days = book_config.teacher_renew_days; // 教师续借天数
            break;
        case USER_TYPE_ADMINISTRATOR:
            max_renew_count = BOOK_MANAGE_CONFIG_MAX_RENEW_TIMES_CONFIG;
            renew_days = book_config.teacher_renew_days; // 管理员续借天数
            break;
        default:
            server_log(LOG_SYSTEM, "Unknown user type for user %s.", user_p->name);
            server_assert(!(user_p->type)); // 断言
            server_assert((user_p->type < USER_TYPE_STUDENT) || (user_p->type > USER_TYPE_ADMINISTRATOR)); // 断言
            return BOOK_UNKNOWN_ERROR; // 未知用户类型，返回
    }
    if (user_p->lent_objects[record_list_number].renew_count > max_renew_count) {
        server_log(LOG_SYSTEM, "User %s has exceeded the maximum renew count for object ID %u.", user_p->name, user_p->lent_objects[record_list_number].object_id);
        return BOOK_RENEW_LIMIT_EXCEEDED; // 续借次数超过限制，返回
    }
    // 更新续借信息
    user_p->lent_objects[record_list_number].renew_count++; // 增加续借次数
    user_p->lent_objects[record_list_number].days_to_return += renew_days; // 更新续借日期

    // 续借成功
    server_log(LOG_INFO, "User %s renewed object ID %u successfully. Renew count: %u, Days to return: %u", 
               user_p->name, user_p->lent_objects[record_list_number].object_id, 
               user_p->lent_objects[record_list_number].renew_count, 
               user_p->lent_objects[record_list_number].days_to_return);
    return BOOK_RENEW_SUCCESS; // 成功续借物品，返回

}

/* 图书馆物品数据加载
    * 该函数用于加载图书馆物品数据
    * @return 如果加载成功返回0，失败或异常返回1
*/
uint8 server_load_object_data(void) {
    server_log(LOG_SYSTEM, "Loading library object data...");
    // 检查物品列表是否已加载
    if (server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list already loaded.");
        return 0; // 已经加载，返回0
    }
    
    // 打开物品数据文件
    FILE * object_data_file = fopen(LIBRARY_OBJECT_DATA_FILE_PATH, "rb");
    if (object_data_file == NULL) {
        server_log(LOG_SYSTEM, "Failed to open library object data file: %s", LIBRARY_OBJECT_DATA_FILE_PATH);
        return 1; // 打开文件失败，返回1
    }
    
    // 读取物品数据
    while (1) {
        // 读取物品数据
        LibraryObject * object = malloc(sizeof(LibraryObject));
        if (object == NULL) {
            server_log(LOG_SYSTEM, "Memory allocation failed while loading library object data.");
            fclose(object_data_file);
            return 1; // 内存分配失败，返回1
        }
        size_t read_size = fread(object, sizeof(LibraryObject), 1, object_data_file);
        if (read_size == 0) {
            free(object); // 释放内存
            break; // 读取结束
        }
        // 创建新的物品节点
        LibraryObjectNode * new_node = malloc(sizeof(LibraryObjectNode));
        if (new_node == NULL) {
            server_log(LOG_SYSTEM, "Memory allocation failed while creating library object node.");
            free(object); // 释放内存
            fclose(object_data_file);
            return 1; // 内存分配失败，返回1
        }
        new_node->object = *object; // 复制物品数据到节点
        new_node->next = NULL;      // 初始化新节点的next指针
        new_node->prev = server_library_object_list.last_node; // 设置前驱节点
        // 将新节点添加到链表中
        if (server_library_object_list.last_node != NULL) {
            server_library_object_list.last_node->next = new_node; // 如果不是第一个节点，设置前驱节点的next指针
        } else {
            server_library_object_list.first_node = new_node; // 如果是第一个节点，设置头指针
        }
        server_library_object_list.last_node = new_node; // 更新尾指针
        server_library_object_list.total_objects++; // 增加物品总数
        free(object); // 释放物品数据内存
    }
    fclose(object_data_file); // 关闭文件
    server_library_object_list_load_flag = 1; // 设置加载标志为1
    server_log(LOG_SYSTEM, "Library object data loaded successfully. Total objects: %llu", server_library_object_list.total_objects);
    return 0; // 成功加载物品数据，返回0
}

/* 图书馆物品数据保存
    * 该函数用于保存图书馆物品数据到文件
    * @return 如果保存成功返回0，失败或异常返回1
*/
uint8 server_save_object_data(void){
    server_log(LOG_SYSTEM, "Saving library object data...");
    // 检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return 1; // 未加载物品列表
    }
    
    // 打开物品数据文件
    FILE * object_data_file = fopen(LIBRARY_OBJECT_DATA_FILE_PATH, "wb");
    if (object_data_file == NULL) {
        server_log(LOG_SYSTEM, "Failed to open library object data file for writing: %s", LIBRARY_OBJECT_DATA_FILE_PATH);
        return 1; // 打开文件失败，返回1
    }
    
    // 遍历物品链表并写入数据
    LibraryObjectNode * current_node = server_library_object_list.first_node;
    while (current_node != NULL) {
        size_t write_size = fwrite(&(current_node->object), sizeof(LibraryObject), 1, object_data_file);
        if (write_size != 1) {
            server_log(LOG_SYSTEM, "Failed to write library object data to file.");
            fclose(object_data_file);
            return 1; // 写入失败，返回1
        }
        current_node = current_node->next; // 移动到下一个节点
    }
    
    fclose(object_data_file); // 关闭文件
    server_log(LOG_SYSTEM, "Library object data saved successfully. Total objects: %llu", server_library_object_list.total_objects);
    return 0; // 成功保存物品数据，返回0
}

// 图书馆物品数据分析
ObjectCountAnalyzeData server_count_analyze_object_data(void){
    server_log(LOG_SYSTEM, "Analyzing library object data...");

    ObjectCountAnalyzeData analyze_data = {0}; // 初始化分析数据

    // 检查物品列表是否已加载
    if (!server_library_object_list_load_flag) {
        server_log(LOG_SYSTEM, "Library object list not loaded yet.");
        server_assert(!server_library_object_list_load_flag); // 重大错误，断言
        return analyze_data; // 未加载物品列表，返回空数据
    }
    
    // 遍历物品链表进行统计
    LibraryObjectNode * current_node = server_library_object_list.first_node;
    while (current_node != NULL) {
        analyze_data.total_objects++; // 增加总物品数量
        analyze_data.total_objects_num += current_node->object.total_num; // 增加物品总数量
        analyze_data.total_remain_objects_num += current_node->object.remain_num; // 增加剩余物品数量
        analyze_data.total_lent_objects_num += (current_node->object.total_num - current_node->object.remain_num); // 增加已借出物品数量
        // 根据物品类型进行分类统计
        switch (current_node->object.type) {
            case OBJECT_TYPE_BOOK:
                analyze_data.total_books++;
                analyze_data.total_books_num += current_node->object.total_num; // 增加图书数量
                analyze_data.total_remain_books_num += current_node->object.remain_num; // 增加剩余图书数量
                analyze_data.total_lent_books_num += (current_node->object.total_num - current_node->object.remain_num); // 增加已借出图书数量
                break;
            case OBJECT_TYPE_PERIODICAL:
                analyze_data.total_periodicals++;
                analyze_data.total_periodicals_num += current_node->object.total_num; // 增加期刊数量
                analyze_data.total_remain_periodicals_num += current_node->object.remain_num; // 增加剩余期刊数量
                analyze_data.total_lent_periodicals_num += (current_node->object.total_num - current_node->object.remain_num); // 增加已借出期刊数量
                break;
            case OBJECT_TYPE_NEWSPAPER:
                analyze_data.total_newspapers++;
                analyze_data.total_newspapers_num += current_node->object.total_num; // 增加报纸数量
                analyze_data.total_remain_newspapers_num += current_node->object.remain_num; // 增加剩余报纸数量
                analyze_data.total_lent_newspapers_num += (current_node->object.total_num - current_node->object.remain_num); // 增加已借出报纸数量
                break;
            default:
                server_log(LOG_SYSTEM, "Unknown object type encountered during analysis.");
                // server_assert(!(current_node->object.type)); // 断言
                break;
        }
        current_node = current_node->next; // 移动到下一个节点
    }
    return analyze_data; // 返回分析结果
}
